Page({
  data: {
    longitude: 116.397958,
    latitude: 39.900140,
    markers: [
      {
        iconPath: "../../image/map_location.png",
        id: '001',
        title:'广西壮族自治区南宁市青秀区东宝路10号骏豪大厦B座5层A室',
        longitude: 108.354320,
        latitude: 22.819580,
        width: 35,
        height: 35
      },
      {
        iconPath: "../../image/map_location.png",
        id: '002',
        title: '广西壮族自治区南宁市青秀区东宝路东一里',
        longitude: 108.353273,
        latitude: 22.819869,
        width: 35,
        height: 35
      },
      {
        iconPath: "../../image/map_location.png",
        id: '003',
        title: '广西壮族自治区南宁市青秀区东宝路3号',
        longitude: 108.350945,
        latitude: 22.819661,
        width: 35,
        height: 35
      },
      {
        iconPath: "../../image/map_location.png",
        id: '004',
        title: '广西壮族自治区南宁市青秀区长堽村南路',
        longitude: 108.352189,
        latitude: 22.828185,
        width: 35,
        height: 35
      },
      {
        iconPath: "../../image/map_location.png",
        id: '005',
        title: '广西壮族自治区南宁市青秀区建政东路39号',
        longitude: 108.348970,
        latitude: 22.827453,
        width: 35,
        height: 35
      },
      {
        iconPath: "../../image/map_location.png",
        id: '006',
        title: '广西壮族自治区南宁市青秀区星湖路39号',
        longitude: 108.345666,
        latitude: 22.809653,
        width: 35,
        height: 35
      },
      {
        iconPath: "../../image/map_location.png",
        id: '007',
        title: '广西壮族自治区南宁市青秀区嘉宾路南一里',
        longitude: 108.352157,
        latitude: 22.824101,
        width: 35,
        height: 35
      },
      {
        iconPath: "../../image/map_location.png",
        id: '008',
        title: '广西壮族自治区南宁市青秀区茶花园路6号',
        longitude: 108.361158,
        latitude: 22.821560,
        width: 35,
        height: 35
      },
      {
        iconPath: "../../image/map_location.png",
        id: '009',
        title: '广西壮族自治区南宁市西乡塘区秀灵路东三里',
        longitude: 108.304639,
        latitude: 22.842997,
        width: 35,
        height: 35
      }
    ],
    controls: [
      {
        id: "current_location",
        iconPath: '../../image/current_location.png',
        position: {
          left: 20,
          top:20,
          width: 35,
          height: 35
        },
        clickable: true
      }
    ]
  },
  markertap(e) {
    console.log(e.markerId)
    for (var index in this.data.markers) {
      if (e.markerId == this.data.markers[index].id) {
        var title = this.data.markers[index].title
        var longitude = this.data.markers[index].longitude
        var latitude = this.data.markers[index].latitude
        wx.showActionSheet({
          itemList: ["启动地图导航"],
          success: res=> {
            console.log(res.tapIndex)
            console.log(longitude)
            console.log(latitude)
            wx.openLocation({
              latitude: latitude,
              longitude: longitude,
              name: title,
              scale: 28
            })
          },
          fail: res => {
            console.log(res.errMsg)
          }
        })
      }
    }
  },
  controltap(e) {
    console.log(e.controlId)
    //回到当前定位点
    this.moveToLocation()
  },
  onReady: function (e) {
    var that = this;
    //定位，获取当前位置坐标
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        var latitude = res.latitude
        var longitude = res.longitude
        console.log( res.longitude)
        console.log( res.latitude)
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        })
      }
    })
    // 使用 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('myMap')
    this.mapCtx.moveToLocation()
  },
  moveToLocation: function () {
    //返回当前位置
    this.mapCtx.moveToLocation()
  },
})